package com.taplinker.client.im.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

public class ConversationDAO {
	private SQLiteDatabase mDataBase = null;
	
	private static final String DB_CONVERSATION_TABLE = "conversation";

	public void getDB(Context context) {
		DatabaseHelper db = new DatabaseHelper(context, DB_CONVERSATION_TABLE, null, 1);
	    mDataBase = db.getWritableDatabase();
	}

	public void insert(Conversation conversation) {
	    ContentValues cv = new ContentValues();
	    cv.put("updateDate", conversation.getUpdateDate().getTime());
	    cv.put("lastMessage",conversation.getLastMessage());
	    cv.put("messageType",conversation.getMessageType());
	    cv.put("csIsLastsayer",conversation.getCsIsLastsayer());
	    cv.put("appName", conversation.getAppName());
	    cv.put("unread",conversation.getUnread());
	    cv.put("csname", conversation.getCsname());
	    cv.put("csicon", conversation.getCsicon());
	    cv.put("productId", conversation.getEntriy().getProductId());
	    cv.put("productName", conversation.getEntriy().getProductName());
	    cv.put("picture",conversation.getEntriy().getPicture());
	    cv.put("description",conversation.getEntriy().getDescription());
	    cv.put("conversationId",conversation.getId());
	    mDataBase.insert(DB_CONVERSATION_TABLE, null, cv);
	}

	public int update(Conversation conversation) {
	    ContentValues cv = new ContentValues();
	    cv.put("updateDate", conversation.getUpdateDate().getTime());
	    cv.put("lastMessage",conversation.getLastMessage());
	    cv.put("messageType",conversation.getMessageType());
	    cv.put("csIsLastsayer",conversation.getCsIsLastsayer());
	    cv.put("appName", conversation.getAppName());
	    cv.put("unread",conversation.getUnread());
	    cv.put("csname", conversation.getCsname());
	    cv.put("csicon", conversation.getCsicon());
	    cv.put("productId", conversation.getEntriy().getProductId());
	    cv.put("productName", conversation.getEntriy().getProductName());
	    cv.put("picture",conversation.getEntriy().getPicture());
	    cv.put("description",conversation.getEntriy().getDescription());
	    cv.put("id",conversation.getId());
	    return mDataBase.update(DB_CONVERSATION_TABLE, cv, "id = ?", new String[]{conversation.getId()});
	}
}
